﻿
Type.registerNamespace('AjaxControlToolkit');


AjaxControlToolkit.TimerExtenderBehavior = function(element) {
    /// <summary>
    /// The TimerExtenderBehavior is used to control the timer
    /// </summary>
    /// <param name="element" type="Sys.UI.DomElement" domElement="true">
    /// DOM element the behavior is associated with
    /// </param>
    AjaxControlToolkit.TimerExtenderBehavior.initializeBase(this, [element]);
    
    this._timer = null;
    
    this._resetOnMouseAction = null;
    this._resetOnKeyPress = null;
    
    this._onmouseaction$delegate = Function.createDelegate(this, this._onmouseaction);
    this._onkeypress$delegate = Function.createDelegate(this, this._onkeypress);
    this._newraiseTick$delegate = Function.createDelegate(this, this._newraiseTick);
}


AjaxControlToolkit.TimerExtenderBehavior.prototype = {
    initialize : function() {
        /// <summary>
        /// Initialize the behavior
        /// </summary>
        AjaxControlToolkit.TimerExtenderBehavior.callBaseMethod(this, 'initialize');
        
        var e = this.get_element();
        
        //get the timer control object
        this._timer = $find(e.id);

        //replace the tickevent
        this._hookTimer(); 
        
        this._resetOnMouseAction = true;
        this._resetOnKeyPress = false;
        
        //add our handlers
        $addHandler(document, 'mousemove', this._onmouseaction$delegate);
        $addHandler(document, 'mousedown', this._onmouseaction$delegate);
        $addHandler(document, 'mouseup', this._onmouseaction$delegate);
        $addHandler(document, 'keypress', this._onkeypress$delegate);
        
    }
    
    ,dispose : function() {
        /// <summary>
        /// Dispose the behavior
        /// </summary>    
        var e = this.get_element();
        
        //remove our handlers
        $removeHandler(document, 'mousemove', this._onmouseaction$delegate);
        $removeHandler(document, 'mousedown', this._onmouseaction$delegate);
        $removeHandler(document, 'mouseup', this._onmouseaction$delegate);
        $removeHandler(document, 'keypress', this._onkeypress$delegate);
        
        //reset variables
        this._onmouseaction$delegate = null;
        this._onkeypress$delegate = null;
        
        this._resetOnMouseAction = null;
        this._resetOnKeyPress = null;
        
        //restore the original tickevent
        this._unhookTimer();
        
        this._timer = null;
        this._newraiseTick$delegate = null;
        
        AjaxControlToolkit.TimerExtenderBehavior.callBaseMethod(this, 'dispose');       
    }
    
    ,_hookTimer : function () {
        /// <summary>
        /// Replace the original value for tickevent in timer control
        /// with our new tickevent
        /// </summary>
            
        //store old tickevent
        this._timer._oldraiseTick = this._timer._raiseTick;
        //if running stop it
        var stopped = !this._timer.get_enabled();
        if (!stopped) {
            this._timer._stopTimer();
        }
        //replace the 'standard' tick event
        this._timer._raiseTick = this._newraiseTick$delegate;  
        //restart the timer if was running
        if (!stopped) {
            this._timer._startTimer();
        }       
    }
    ,_unhookTimer : function () {
        /// <summary>
        /// Restore original value for tickevent in timer control
        /// </summary>
            
        //replace original values
        var stopped = !this._timer.get_enabled();
        //if running stop it
        if (!stopped) {
            this._timer._stopTimer();
        }
        //replace the 'standard' tick event
        this._timer._raiseTick = this._timer._oldraiseTick;  
        //restart the timer if was running
        if (!stopped) {
            this._timer._startTimer();
        }       
        //free variable
        this._timer._oldraiseTick = null;
    }
     
    ,_newraiseTick : function() {
        /// <summary>
        /// Handler invoked when a timer tick occurs
        /// </summary>
        var eventArgs = new Sys.CancelEventArgs();
        this.raiseClientTick(eventArgs);
        if (eventArgs.get_cancel()) {
            if ( this._timer.get_enabled() ) 
                    this._timer._startTimer();
            return false;
        }
        this._timer._oldraiseTick();
    }
    
    ,_onmouseaction : function() {
        if (!this._resetOnMouseAction || !this._timer.get_enabled()) return;
        this._timer._stopTimer();
        this._timer._startTimer();
    }
        
    ,_onkeypress : function() {        
        if (!this._resetOnKeyPress || !this._timer.get_enabled()) return;
        this._timer._stopTimer();
        this._timer._startTimer();        
    }
    
    ,add_clientTick : function(handler) {
        /// <summary>
        /// Add an event handler for the tick event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />    
        this.get_events().addHandler("clientTick", handler);
    }
    ,remove_clientTick : function(handler) {
        /// <summary>
        /// Remove an event handler from the tick event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />    
        this.get_events().removeHandler("clientTick", handler);
    }
    ,raiseClientTick : function(eventArgs) {
        /// <summary>
        /// Raise the tick event
        /// </summary>
        /// <param name="eventArgs" type="Sys.CancelEventArgs" mayBeNull="false">
        /// Event arguments for the tick event
        /// </param>
        /// <returns />
        /// <remarks>
        /// The tick event can be used to cancel tick event for the timer control
        /// Just cancel the event using the CancelEventArgs.
        /// </remarks>
        var handler = this.get_events().getHandler('clientTick');
        if (handler) {
            handler(this, eventArgs);
        }
    }
     
    ,get_resetOnMouseAction : function() {
        /// <value type="Boolean" maybeNull="true">
        /// Gets if timer control is reset when mouse action is raised.
        /// </value>    
        return this._resetOnMouseAction;
    }
    ,set_resetOnMouseAction : function(value) {
        if (this._resetOnMouseAction != value) {
            this._resetOnMouseAction = value;
            this.raisePropertyChanged("resetOnMouseAction");
        }    
    }
    
    ,get_resetOnKeyPress : function() {
        /// <value type="Boolean" maybeNull="true">
        /// Gets if timer control is reset when a keypress is raised.
        /// </value>      
        return this._resetOnKeyPress;
    }
    ,set_resetOnKeyPress : function(value) {
        if (this._resetOnKeyPress != value) {
            this._resetOnKeyPress = value;
            this.raisePropertyChanged("resetOnKeyPress");
        }    
    }    
}    

AjaxControlToolkit.TimerExtenderBehavior.registerClass('AjaxControlToolkit.TimerExtenderBehavior', AjaxControlToolkit.BehaviorBase);
